package gameobjects;

import java.awt.Graphics;
import abstracts.Level;
import resources.Sound;
import resources.Sprite;
import tools.Bounds;
import tools.Statistics;
import interfaces.Collidable;
import interfaces.Visible;

public class Treasure implements Visible, Collidable {
    public enum Type {
        DIAMOND(Sprite.TREASURE_DIAMOND, Sound.TREASURE_JEWEL, 500),
        RUBY(Sprite.TREASURE_RUBY, Sound.TREASURE_JEWEL, 200),
        EMERALD(Sprite.TREASURE_EMERALD, Sound.TREASURE_JEWEL, 100),
        SAPPHIRE(Sprite.TREASURE_SAPPHIRE, Sound.TREASURE_JEWEL, 50);

        private Sprite sprite;
        private Sound sound;
        private long value;

        private Type(Sprite sprite, Sound sound, long value) {
            this.sprite = sprite;
            this.sound = sound;
            this.value = value;
        }
    }

    protected Level level;
    protected Bounds bounds;

    private Type type;

    public Treasure(Level level, int row, int col, String type) {
        this.level = level;
        bounds = new Bounds(row, col, 24, 24);
        this.type = Type.valueOf(type.toUpperCase());
    }

    @Override
    public final void draw(final Graphics g) {
        type.sprite.drawAnchored(g, 0, bounds);
    }

    @Override
    public final double getDepth() {
        return bounds.getMaxY();
    }

    @Override
    public final Bounds getBounds() {
        return bounds;
    }

    /**
     * Called when the mage collides with the treasure.
     */
    public final void collect() {
        type.sound.play();
        level.remove(this);
        Statistics.treasure += type.value;

        // Animation
        TaskQueue tasks = new TaskQueue();
        tasks.add(new ShrinkAnimation(bounds, type.sprite, 0.0625));
        level.add(tasks);
        tasks.run(level);
    }
}
